package mapDemo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class Demo6 {
	public static void main(String[] args) {
		String string = "aaabbbcccceeedgeg";
		Map<Character, Integer> map = count(string);
		Set<Entry<Character, Integer>> set=map.entrySet();
		ArrayList<Entry<Character, Integer>> alist = new ArrayList<Map.Entry<Character,Integer>>(set);
		Collections.sort(alist,new ByValue1());
		Iterator<Entry<Character, Integer>> iterator = alist.iterator();
		while(iterator.hasNext()){
			Entry<Character, Integer> entry = iterator.next();
			System.out.println(entry.getKey());
			System.out.println(entry.getValue());
			System.out.println((double)entry.getValue()/string.length()*100);
		}
	}
	
	public static Map<Character, Integer> count(String s){
		Map<Character, Integer> map = new HashMap<Character, Integer>();
		for(int i=0;i<s.length();i++){
			char c = s.charAt(i);
			Integer num = map.get(c);
			num= num==null?1:num+1;
			map.put(c, num);
		}
		return map;
	}
}

class ByValue1 implements Comparator<Entry<Character, Integer>>{

	@Override
	public int compare(Entry<Character, Integer> o1,
			Entry<Character, Integer> o2) {
		// TODO Auto-generated method stub
		return (Integer)o1.getValue()-(Integer)o2.getValue();
	}
	
}

